(function ($) {

})(jQuery);
// 停止录制，可以在适当的时间点调用
// stopRecording();
/**全屏 */
/**公共事件 */
function bindComEvent() {
  //按钮组点击事件
  $(".btn-group").off('click', '.btn').on('click', '.btn', function () {
    $(this).addClass('active').siblings().removeClass('active');
  });
  //筛选
  $(".showMoreFilter").length &&
    $(".showMoreFilter")
      .off("click")
      .on("click", function () {
        $(this).parents(".searchBox").children(".searchForm").slideToggle();
      });
  $(".searchFormClose").length &&
    $(".searchFormClose")
      .off("click")
      .on("click", function () {
        $(this).parent(".searchForm").slideUp();
      });

  $(".comSwitch").length &&
    $(".comSwitch")
      .off("click")
      .on("click", function () {
        $(this).toggleClass("on");
        $(this).toggleClass("off");
      });
  $(".comTabList").length && $(".comTabList")
    .off("click", '.item')
    .on("click", '.item', function () {
      $(this).addClass('active').siblings().removeClass('active')
    });
}
/**下拉选择事件绑定 */
function bindSelectEvent() {
  $(".selectpicker").on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
    var val = $(this).val();
    if (val) {
      $(this).parent().find('.close-select').fadeIn();
    }
  })
  $(".selectpicker").on('loaded.bs.select', function () {
    var val = $(this).val();
    if (val) {
      $(this).parent().find('.close-select').fadeIn();
    }
  })
  $(document).off('click', '.close-select').on('click', '.close-select', function (e) {
    e.stopPropagation();
    $(this).parent('button').prev('select').selectpicker('val', '');
    $(this).fadeOut();
  });
}
/**跳转 */
function loadHtml(url, callback, title) {
  var dom = "mainIframe";
  if (url) {
    $("#" + dom).load(url, function () {
      if (callback) {
        callback();
      }
    });
  } else {
    $("#" + dom).html(
      "<div style='font-size:30px;text-align:center;line-height:200px;color:red;' >正在努力建设中......</div>"
    );
  }
}
function setUrl(url) {
  var urlObj = {
    url: '',
    params: {}
  };
  if (url.indexOf("?") != -1) {
    var _arr = url.split('?');
    urlObj.url = _arr[0];
    /*var _params = _arr[1] ? _arr[1].split('&') : [];
    _params.filter(function (v) {
      var objArr = v.split('=');
      urlObj.params[objArr[0]] = objArr[1];
    })*/
  } else {
    urlObj.url = url;
  }
  return urlObj.url;
}
function compantSetUrl(url) {
  var urlObj = {
    url: '',
    params: {}
  };
  if (url.indexOf("?") != -1) {
    var _arr = url.split('?');
    urlObj.url = _arr[0];
    var _params = _arr[1] ? _arr[1].split('&') : [];
    _params.filter(function (v) {
      var objArr = v.split('=');
      urlObj.params[objArr[0]] = objArr[1];
    })
  } else {
    urlObj.url = url;
  }
  return urlObj.url;
}
function getIframeId(type) {
  var iframs = $("#mainIframe > .iframe", parent.document);
  var _data = iframs.eq(0).data(type);
  $.each(iframs, function (i, v) {
    if (!$(v).is(":hidden")) {
      _data = $(v).data(type);
    }
  });
  return _data;
}
function CompanyAddTab(options, pid) {
  var title = options.name,
    id = options.id,
    href = options.path,//compantSetUrl(options.path),
    data = options.data || null,
    callback = options.callback || null;
  if (!href) {
    return false;
  }
  var htm = "";
  var child = $("#companyHeadTree", parent.document).children("a");
  var hasChild = false;
  var index = 0,
    a_ind = 0;
  var c_href = href;
  if (child.length > 0) {
    htm =
      '<a class="active" href="javascript:;" data-pid="' + pid + '" data-id="' +
      id +
      '" data-url="' +
      href +
      '" title="' +
      title +
      '">' +
      title +
      ' <i class="iconfont icon-close-X" onclick="CompanyRemoveTab(this)"></i></a>';
    for (var i = 0; i < child.length; i++) {
      var _a = child.eq(i);
      var _url = _a.data("url");
      var a_href = _url;
      if (_a.hasClass("active")) {
        a_ind = i;
      }
      if (a_href == c_href) {
        hasChild = true;
        _a.addClass("active");
        index = i;
        var s = $("#menuBox [data-name='" + title + "']", parent.document).eq(
          0
        );
        //console.log(s);
        if (s) {
          $("#menuBox", parent.document).find(".active").removeClass("active");
          $("#menu_" + options.id, parent.document).parent().addClass("active");
          var p = s.parent().prev().children().eq(0);
          if (!p.hasClass("active")) {
            p.addClass("active");
          }
        }
      } else {
        _a.removeClass("active");
      }
    }
    // $("#navHome", parent.document).removeClass("active");
    if (hasChild) {
      $("#mainIframe", parent.document)
        .children(".iframe")
        .eq(index)
        .removeClass("hidden")
        .siblings()
        .addClass("hidden");
    } else {
      child.eq(a_ind).after(htm);
    }
  } else {
    htm =
      '<a class="active" href="javascript:;" data-pid="' + pid + '" data-id="' +
      id +
      '" data-url="' +
      href +
      '" title="' +
      title +
      '">' +
      title +
      '<i class="iconfont icon-close-X" onclick="CompanyRemoveTab(this)"></i></a>';
    $("#companyHeadTree", parent.document).append(htm);
  }
  var p_w = $("#companyHeadTree").parent().width();
  var t_w = $("#companyHeadTree").width();
  if (t_w > p_w) {
    $("#companyHeadTree").css('left', '-' + (t_w - p_w) + 'px');
    $("#companyHeadTree").parent().siblings('.treeLeft').removeClass('disabled');
    $("#companyHeadTree").parent().siblings('.treeRight').removeClass('disabled');
  } else {
    $("#companyHeadTree").parent().siblings('.treeLeft').addClass('disabled');
    $("#companyHeadTree").parent().siblings('.treeRight').addClass('disabled');
  }

  $("#companyHeadTree a", parent.document)
    .off("click")
    .on("click", function () {
      var ind = $(this).index();
      var id = $(this).data("id");
      var pid = $(this).data("pid");
      CompanyInitMenuView(id, pid);
      $(this).addClass("active").siblings().removeClass("active");
      $("#mainIframe", parent.document)
        .children(".iframe")
        .addClass("hidden")
        .eq(ind)
        .removeClass("hidden");
      // CompanyAddTab(title, href);
    });
  //处理传参
  if (data) {
    href += href.indexOf("?") != -1 ? "&" : "?";
    var sparam = "";
    $.each(data, function (k, v) {
      if (sparam.length > 0) sparam += "&";
      sparam += k + "=" + v;
    });
    href += sparam;
  }

  // $.ajax({
  // 	url: src,
  // 	cache: true,
  // 	type: 'get',
  // 	dataType: 'html',
  // 	success: function(res) {
  var randomTime = new Date().getTime();
  href += href.indexOf("?") >= 0 ? "&" : "?";
  href += randomTime;
  href = szrcTargetUrl2(href)
  var html =
    '<iframe id="iframe' +
    randomTime +
    '" class="iframe" data-url="' +
    href +
    '" width="100%" height="100%" frameBorder="0" src="' +
    href +
    '"></iframe>';
  var myframe = $("#mainIframe", parent.document).children(".iframe");
  if (hasChild) {
    // $.each(myframe, function (i, v) {
    // 	var n_url = $(v).data('url')
    // 	var n_href = setUrl(n_url)
    // 	if (n_href == c_href) {
    // 		$(v).html(res)
    // 		$(v).removeClass('hidden')
    // 		// $(v).append("<div class='red'>我刷行了</div>")
    // 	}
    // })
  } else {
    $("#mainIframe", parent.document).children(".iframe").addClass("hidden");
    if (myframe.length > 0) {
      myframe.eq(a_ind).after(html);
    } else {
      $("#mainIframe", parent.document).append(html);
    }
  }
  if (callback && typeof callback == "function") {
    callback(data);
  }
}
function CompanyRemoveTab(obj, url) {
  var child = $("#companyHeadTree", parent.document).children("a");
  if (child && child.length == 1) return;
  var $a = $(obj).parent("a");
  var ind = $a.index();
  var next = $a.next();
  var prev = $a.prev();
  var id = "", pid = '';
  if ($a.hasClass("active")) {
    if (url) {
      var ts = $("#companyHeadTree > a", parent.document);
      ts.forEach(function (v, i) {
        var dom = $(v);
        if (dom.data("url") == url) {
          dom.addClass("active");
          id = dom.data("id");
          pid = dom.data("pid");
          $("#mainIframe", parent.document)
            .find(".iframe")
            .eq(i)
            .removeClass("hidden");
        } else {
          dom.removeClass("active");
        }
      });
    } else {
      if (prev.length > 0) {
        id = prev.data("id");
        pid = prev.data("pid");
        prev.addClass("active");
        $("#mainIframe", parent.document)
          .find(".iframe")
          .eq(prev.index())
          .removeClass("hidden");
      } else if (next.length > 0) {
        id = next.data("id");
        pid = next.data("pid");
        next.addClass("active");
        $("#mainIframe", parent.document)
          .find(".iframe")
          .eq(next.index())
          .removeClass("hidden");
      } else {
        // $("#defaultHome").removeClass('hidden');
      }
    }
    CompanyInitMenuView(id, pid);
  }
  var tab_url = $a.data("url");
  var menu_url = $("#menuBox").find(".active").data("src");
  if (menu_url == tab_url) {
    $("#menuBox").find(".active").removeClass("active");
  }
  var p_w = $("#companyHeadTree").parent().width();
  var t_w = $("#companyHeadTree").width();
  var left = $("#companyHeadTree").position().left;
  var m_l = Math.abs(left)
  if (t_w < p_w) {
    $("#companyHeadTree").css('left', '0');
    $("#companyHeadTree").parent().siblings('.treeLeft').addClass('disabled');
    $("#companyHeadTree").parent().siblings('.treeRight').addClass('disabled');
  } else {
    var m_l = m_l - m_w
    if (m_l < 0) {
      m_l = 0
    }
    $("#companyHeadTree").css('left', '-' + m_l + 'px');
    $("#companyHeadTree").parent().siblings('.treeLeft').removeClass('disabled');
    $("#companyHeadTree").parent().siblings('.treeRight').removeClass('disabled');
  }
  $("#mainIframe", parent.document).children(".iframe").eq(ind).remove();
  $a.remove();
}
function CompanyInitMenuView(id, pid) {
  var a_menus = $("#asidMenu .menu_item", parent.document);
  var child_w = $("#child_menu_box", parent.document).width();
  $.each(a_menus, function (i, dom) {
    var dom_id = $(dom).data("id");
    dom_id = dom_id ? dom_id * 1 : '';
    if (pid == dom_id) {
      $(dom, parent.document).trigger('click');
      $(dom, parent.document).addClass('active').siblings().removeClass('active');
      child_w = $("#child_menu_box", parent.document).width();
    }
  });
  $.each($("#child_menu_list a"), function (i, dom) {
    var dom_id = $(dom).attr("id");
    dom_id = dom_id ? dom_id * 1 : '';
    if (id == dom_id) {
      // $(dom, parent.document).trigger('click');
      if ($(dom).parent('li').length) {
        if (!$(dom).parent('li').hasClass('showChild')) {
          $(dom).parent('li').trigger('click');
        }
      }
      $(dom, parent.document).find('.itemLink').removeClass('active');
      $(dom, parent.document).addClass('active')

    }
  });
}
/**头部菜单操作 */
function closeHeadTree(type) {
  var trees = $("#companyHeadTree a");
  var active = $("#companyHeadTree a.active")
  var ind = active.index();
  var id = active.data("id");
  if (trees.length == 1) {
    return false
  }
  if (ind == 0) {
    ind++
  } else {
    ind--
  }
  if (type == 'all') {
    trees.eq(0).trigger('click');
    trees.eq(0).nextAll().remove();
    $("#companyHeadTree").css('left', '0');
    $("#mainIframe", parent.document).children(".iframe").eq(0).nextAll().remove();
  } else if (type == 'other') {
    active.siblings().remove();
    $("#companyHeadTree").css('left', '0');
    $("#mainIframe", parent.document).children(".hidden").remove();
  } else {
    CompanyRemoveTab(active.children('i'))
  }
}
function bindTreeEvent() {
  $("#companyTreeBox").off('click', '.treeLeft').on('click', '.treeLeft', function () {
    var p_w = $("#companyTreeBox .treeMain").width();
    var t_w = $("#companyHeadTree").width();
    var t_l = $("#companyHeadTree").position().left;
    var r_w = t_w - p_w;
    var m_l = Math.abs(t_l)
    if (t_w < p_w) {
      return false;
    }
    if (t_l == 0) {
      return false;
    } else {
      if (m_l <= (p_w / 2)) {
        $("#companyHeadTree").css('left', 0)
      } else {
        $("#companyHeadTree").css('left', '-' + p_w / 2 + 'px')
      }
    }
  })
  $("#companyTreeBox").off('click', '.treeRight').on('click', '.treeRight', function () {
    var p_w = $("#companyTreeBox .treeMain").width();
    var t_w = $("#companyHeadTree").width();//
    var t_l = $("#companyHeadTree").position().left;//偏移值
    var m_l = Math.abs(t_l)
    var r_w = t_w - p_w;//差值
    if (t_w < p_w) {
      return false;
    }
    if (t_l > m_l) {
      return false;
    } else {
      if ((r_w - m_l) >= p_w / 2) {
        $("#companyHeadTree").css('left', '-' + (t_l + p_w / 2) + 'px')
      } else {
        $("#companyHeadTree").css('left', '-' + r_w + 'px')
      }
    }
  })
}
/**loading */
function loadOpen() {
  $("#loadingBox").fadeIn();
}
function loadClose() {
  $("#loadingBox").fadeOut();
}
function szrcTargetUrl2(target) {
  target += target.indexOf("?") >= 0 ? "&" : "?";
  // target += "jsessionid=" + szrcGetLoginToken();
  return target;
}
/**
 * 防抖节流
 * @param {*} action 回调
 * @param {*} delay 等待的时间
 * @param {*} context this指针
 * @param {Boolean} iselapsed 是否等待上一次
 * @returns {Function}
 */
function throttle(action, delay, context, iselapsed) {
  var timeout = null;
  var lastRun = 0;
  return function () {
    if (timeout) {
      if (iselapsed) {
        return;
      } else {
        clearTimeout(timeout);
        timeout = null;
      }
    }
    var elapsed = Date.now() - lastRun;
    var args = arguments;
    if (iselapsed && elapsed >= delay) {
      runCallback();
    } else {
      timeout = setTimeout(runCallback, delay);
    }
    /**
     * 执行回调
     */
    function runCallback() {
      lastRun = Date.now();
      timeout = false;
      action.apply(context, args);
    }
  };
}

/*上传附件单独的显示附件名称*/
function InitFileView(file, id) {
  if (file.files && file.files[0]) {
    var _file = file.files[0];
    var reader = new FileReader();
    reader.readAsDataURL(_file);
    reader.onload = function (evt) {
      $("#" + id)
        .children("button")
        .hide();
      $("#" + id)
        .children(".fileName")
        .show()
        .children(".name")
        .text(_file.name);
    };
  }
}
/**
 * 上传图片   返回base64
 * @param {obj} file input选择的文件
 * @param {string} id 存放图片的地方
 * @param {string} type 上传图片位置
 */
function preview(file, id, onePic = false) {
  var prevDiv = $("#" + id);
  if (file.files && file.files[0]) {
    var _file = file.files[0];
    var _calss = "";
    var image = new Image();
    image.src = URL.createObjectURL(_file);
    image.onload = function () {
      var width = image.width,
        height = image.height;
      if (width > height) {
        _class = "fullHeight";
      } else {
        _class = "fullWidth";
      }
      var reader = new FileReader();
      reader.readAsDataURL(_file);
      reader.onload = function (evt) {
        console.log(evt);
        if (onePic) {
          $("#" + id)
            .find(".fileItem")
            .fadeOut();
        }
        var li =
          '<li class="item">' +
          '<img class="' +
          _class +
          '" src="' +
          evt.target.result +
          '"/>' +
          '<i class="closePic iconfont icon-close-rs" onclick="deletePic(this,' +
          onePic +
          ')"></i>' +
          "</li>";
        prevDiv.prepend(li);
      };
    };
  } else {
    var li =
      '<li class="item">' +
      "<div style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src='" +
      file.value +
      "'\"></div>" +
      '<i class="closePic layui-icon layui-icon-close-fill" onclick="deletePic(this,' +
      onePic +
      ')"></i></li>';
    prevDiv.prepend(li);
  }
}
/**删除上传的图片展示 */
function deletePic(obj, onePic) {
  if (onePic) {
    $(obj).parent("li").siblings(".fileItem").fadeIn();
  }
  $(obj).parent("li").remove();
}
/**展示大图 */
function showBigPIc(obj) {
  var src = $(obj).data("src");
  if (src) {
    var html = '<div class="bigPicBox">';
    html +=
      '<div class="bigPicModal"><img src="' +
      src +
      '"/><i class="closePic layui-icon layui-icon-close-fill" onclick="hideBigPic(this)"></i><div>';
    html += "</div>";
    $("body", parent.document).append(html);
  }
}
/**删除大图 */
function hideBigPic(obj) {
  $(obj).parents(".bigPicBox").remove();
}

/**
 * 
 * @param {string} value 传入的时间
 * @param {string} dateType 返回的时间格式
 * @param {string/number} timeType 返回的天数区间
 */
function getDate(value, dateType, timeType) {
  var _date = new Date();
  if (value) {
    _date = new Date(value);
  }
  if (typeof (timeType) == 'number') {
    var times = _date.getTime() - timeType * 24 * 60 * 60 * 1000
    _date = new Date(times);
  }

  var year = _date.getFullYear(); // 获取当前年份
  var month = _date.getMonth() + 1; // 获取当前月份，注意月份从0开始，需要加1
  var day = _date.getDate(); // 获取当前日期
  var hour = _date.getHours();
  var min = _date.getMinutes();
  var second = _date.getSeconds();
  if (timeType == 'lastday') {
    day = new Date(year, month + 1, 0).getDate();
  } else if (timeType == 'firstday') {
    day = 1;
  }
  month = month < 10 ? '0' + month : month;
  day = day < 10 ? '0' + day : day;
  hour = hour < 10 ? '0' + hour : hour;
  min = min < 10 ? '0' + min : min;
  second = second < 10 ? '0' + second : second;
  var res = year + '-' + month + '-' + day
  switch (dateType) {
    case 'datetime':
      res += ' ' + hour + ':' + min + ':' + second
      break;
    case 'datehourmin':
      res += ' ' + hour + ':' + min
      break;
    case 'yearmonthZH':
      res = year + '年' + month + '月' + day + '日'
    default:
      res = year + '-' + month + '-' + day
      break;
  }
  return res;
};
// 创建一个函数来生成指定年份、月份的日历
function generateCalendar(year, month) {
  // 计算该月第一天是星期几（0-6表示周日到周六）
  var firstDay = new Date(year, month - 1, 1).getDay();

  // 根据每个月有多少天确定最后一行显示的空白格子数量
  var lastDayOfMonth = new Date(year, month, 0).getDate();
  var numBlankCells = (firstDay + lastDayOfMonth > 35) ? 7 : 8;

  // 输出日历头部信息
  console.log(firstDay);
  console.log(lastDayOfMonth);
  console.log(numBlankCells);

  // 打印上个月未完全展示的日期
  for (var i = 0; i < firstDay; i++) {
    // console.log(firstDay[i]);
  }

  // 遍历并打印本月所有日期
  for (var j = 1; j <= lastDayOfMonth; j++) {
    if ((j + firstDay) % 7 === 0 || (j + firstDay) % 7 === 1 && j !== 1) {
      console.log('\n');
    } else {
      console.log((j < 10) ? (' ' + j) : j + ' ');
    }
  }

  // 打印下个月未完全展示的日期
  for (var k = 1; k <= numBlankCells; k++) {
    // console.log(numBlankCells[k]);
  }
}
function refreshPage() {
  $.each($("#mainIframe iframe"), function (i, dom) {
    if (!$(dom).hasClass('hidden')) {
      $(dom)[0].contentWindow.location.reload();
    }
  })
}
function numberToChinese(num) {
  var units = ['', '十', '百', '千', '万', '十万', '百万', '千万', '亿'];
  var digits = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九'];

  var result = String(num).replace(/./g, function (digit, index, array) {
    return digits[Number(digit)] + units[array.length - index - 1];
  });

  return result;
}
// generateCalendar(new Date().getFullYear(), new Date().getMonth() + 1);